<template>
	<view class="editor-box">
		<cu-editor ref="editor" :url="uploadUrl" :header="header" :formData="formData" :content="content"
			:titleValue="title" @before="onUploadBefore" @update="onUpdate" @save="onSave"
			@titleValueMethod="titleValue" @dateTimeMethod="dateTimeMethod" :loading="submitLoad"></cu-editor>
		<!-- <view class="fabBtn" @click="timeClick">
			<img src="https://file.casugn.com/storecompcard//5HFth22GLV9Ca40701d3b15ba795684d387e231a42dc_1661994583008.png" alt="">
		</view>
		 <u-datetime-picker :show="dateTimeShow" v-model="dateTimeVal" mode="datetime" @confirm="dateTimeConfirm" closeOnClickOverlay @close="dateTimeShow=false" @cancel="dateTimeShow=false"></u-datetime-picker> -->

	</view>
</template>

<script>
	import baseConfig from '@/config/baseUrl.js'
	import cuEditor from '@/pagesCard/memorandumInfo/cu-editor/cu-editor.vue'
	import {
		handleHtmlImage
	} from '@/components/cu-editor/util'
	export default {
		components: {
			cuEditor
		},
		data() {
			return {
				content: '',
				uploadUrl: baseConfig.uploadUrl + '/sys/common/upload',
				header: {},
				formData: {},
				// dateTimeShow:false,
				// dateTimeVal:Number(new Date()),
				dateTime: '',
				isSave: false,
				submitLoad: false, //提交加载
				id: '',
				openId: '',
				title: '',
				dateTimeVal: '',
				addValue: false, //true--代表得是添加，无内容，false代表得是修改 有内容

			}
		},
		onLoad(options) {
			this.isSave = false
			// console.log(options.item)
			if (options.item) {
				// console.log(decodeURIComponent(options.item))
				const item = JSON.parse(decodeURIComponent(options.item));
				console.log(item.title)
				item.content ? this.content = item.content : ''
				item.title ? this.title = item.title : ''
				item.startTime ? this.dateTime = item.startTime : ''
				item.id ? this.id = item.id : ''
			} else {

			}
			if (this.isSave == 'false') {
				wx.enableAlertBeforeUnload({
					message: "当前信息尚未保存，退出将会丢失，是否退出？"
				})
			}
			let openId = uni.getStorageSync('openId')
			console.log("这是openId", openId)
			this.openId = openId.openid
			if (options.addType) {
				this.addValue = true
			} else {
				this.addValue = false
			}

		},
		beforeDestroy() {
			console.log("执行了么")
			console.log(this.isSave)
			if (this.isSave == 'false') {
				wx.enableAlertBeforeUnload({
					message: "当前信息尚未保存，退出将会丢失，是否退出？"
				})
			}
		},
		methods: {
			dateTimeMethod(val) {
				this.dateTime = val
			},
			titleValue(val) {
				this.title = val
			},
			onUploadBefore: function() {
				this.formData = {
					timestamp: (Date.parse(new Date())) / 1000
				}
			},
			onUpdate(res) {
				res.html = handleHtmlImage(res.html)
				this.value = res.html
				// console.log(this.value)
			},
			//保存
			onSave(e) {
				if (this.title == '') {
					uni.showToast({
						icon: 'none',
						title: '标题不能为空'
					})
					return false
				}
				this.isSave = true
				let html = e.html
				var containsImage = html.search(/<img /i) >= 0 //内容是否包含图片标签
				let txt = e.text.replace(/(^\s*)|[\r\n]|(\s*$)/g, "") //去掉换行符和两端空格
				let data = {
					openId: this.openId,
					title: this.title,
					startTime: this.dateTime,
					descn: html
				}
				if (this.id) data.id = this.id
				let method = this.id ? 'editMyMemoInfo' : 'addMyMemoInfo'
				if (this.submitLoad === true) return
				this.submitLoad = true
				this.$api[method](data).then(res => {
					if (res.success) {
						uni.navigateBack()
					} else {
						this.$showToast(res.message)
					}
					this.submitLoad = false
				})

			}


		},
		// // 时间选择
		// timeClick(){
		// 	console.log("时间选择")
		// 	this.dateTimeShow=true
		// },
		// // 时间戳转yy-mm-dd hh-mm-ss
		// shijianc(time) {
		// 	var date = new Date(time)
		// 	console.log("测试" + date)
		// 	var Y = date.getFullYear() + '-'
		// 	var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
		// 	var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
		// 	var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
		// 	var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
		// 	// + ':'
		// 	var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
		// 	return Y + M + D + h + m
		// },
		// dateTimeConfirm(val){
		// 	console.log(val.value)
		// 	let a=this.shijianc(val.value)
		// 	this.dateTimeShow=false
		// 	uni.showToast({
		// 		title: "已设置提醒时间为\n"+a,
		// 		icon: 'none'
		// 	});
		// 	console.log(a)

		// },
	}
</script>

<style lang="scss" scoped>
	.editor-box {
		width: 100%;
		margin: 0 auto;
		position: relative;

		// background: red;
		.fabBtn {
			position: fixed;
			width: 120upx;
			height: 120upx;
			text-align: center;
			background: #5677fc;
			// box-shadow: 0upx 3upx 20upx 0upx rgba(65, 59, 243, 0.5000);
			border-radius: 50%;
			right: 30upx;
			bottom: 150upx;
			z-index: 80;

			img {
				width: 50upx;
				height: 50upx;
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translateX(-25upx) translateY(-25upx);
			}
		}
	}
</style>